package com.css.fxfzysx.modules.ysxManagement.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.vabStructure.repository.PobStructureRepository;
import com.css.fxfzysx.modules.ysxManagement.entity.DynamicFieldsEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.ZqtEntity;
import com.css.fxfzysx.modules.ysxManagement.repository.ZqtCiRepository;
import com.css.fxfzysx.modules.ysxManagement.service.ZqtCiService;
import com.css.fxfzysx.modules.ysxManagement.vo.ZqtVo;
import com.css.fxfzysx.util.PlatformDateUtils;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.css.fxfzysx.util.PlatformSessionUtils;
import com.css.fxfzysx.util.UUIDGenerator;
import com.css.fxfzysx.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ZqtCiServiceImpl implements ZqtCiService {

    @Autowired
    private ZqtCiRepository zqtCiRepository;

    @Autowired
    private PobStructureRepository pobStructureRepository;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse saveOrUpdateZqtCi(ZqtVo zqtVo) {
        try {
            List<ZqtEntity> zqtEntity = zqtVo.getZqtEntity();
            List<DynamicFieldsEntity> dynamicFieldsEntity = zqtVo.getDynamicFieldsEntity();
            if (PlatformObjectUtils.isNotEmpty(zqtEntity)) {
                zqtEntity.forEach(it -> {
                    if (PlatformObjectUtils.isEmpty(it.getCbrmId())) {
                        it.setCbrmId(UUIDGenerator.getUUID());
                        it.setCreateUser(PlatformSessionContext.getUserID());
                        it.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                        it.setDelFlag("0");
                        it.setDefaultData("1");
                        zqtCiRepository.saveZqt(it);
                    } else {
                        it.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                        it.setUpdateUser(PlatformSessionUtils.getUserId());
                        List<ZqtEntity> list = zqtCiRepository.getZqtById(it.getCbrmId());
                        list.forEach(zqt->{
                            if (!zqt.getCiValue().equals(it.getCiValue())){
                                pobStructureRepository.updateCountStatusById(it.getCbrmId());
                            }
                        });
                        zqtCiRepository.updateZqtCi(it);
                    }
                });
            }
            saveOrUpdateDynamicFields(dynamicFieldsEntity);
            return RestResponse.succeed("成功!");
        } catch (Exception e) {
            String errorMessage = "失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }


    @Override
    public RestResponse getAllZqtCi(String titleName) {
        try {
            ZqtVo zqtVo = new ZqtVo();
            List<ZqtEntity> zqtList = zqtCiRepository.getZqt();
            List<DynamicFieldsEntity> dynamicFields = zqtCiRepository.getDynamicFields(titleName);
            zqtVo.setZqtEntity(zqtList);
            zqtVo.setDynamicFieldsEntity(dynamicFields);
            return RestResponse.succeed(zqtVo);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public void saveOrUpdateDynamicFields(List<DynamicFieldsEntity> dynamicFieldsList) {
        if (PlatformObjectUtils.isNotEmpty(dynamicFieldsList)) {
            dynamicFieldsList.forEach(it -> {
                if (PlatformObjectUtils.isEmpty(it.getCbrsId())) {
                    it.setCbrsId(UUIDGenerator.getUUID());
                    it.setDelFlag("0");
                    it.setDefaultData("1");
                    it.setCreateUser(PlatformSessionUtils.getUserId());
                    it.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                    zqtCiRepository.saveDynamicFields(it);
                } else {
                    it.setUpdateUser(PlatformSessionUtils.getUserId());
                    it.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                    zqtCiRepository.updateDynamicFields(it);
                }
            });
        }
    }

    @Override
    public RestResponse deleteZqtCiById(String zqtIds, String arrangeIds) {
        try {
            if (PlatformObjectUtils.isNotEmpty(zqtIds)) {
                for (String zptId : zqtIds.split(",")) {
                    zqtCiRepository.deleteZqtCiById(zptId);
                }
            }
            if (PlatformObjectUtils.isNotEmpty(arrangeIds)) {
                for (String arrangeId : arrangeIds.split(",")) {
                    zqtCiRepository.deleteArrangeById(arrangeId);
                    //删除数据
                    zqtCiRepository.deleteZqtCiByProjectId(arrangeIds);
                }
            }
            return RestResponse.succeed("删除成功!");
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }
}
